//
//  GYHomeBuyDetailVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/3.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYHomeBuyDetailVC.h"
#import "Head.h"
#import "DWSegmentedControl.h"
#import "GYhomeBuyDetailViewModel.h"
#import "GYBuyDetailView.h"
#import "GYBuyDetailListView.h"
@interface GYHomeBuyDetailVC ()<DWSegmentedControlDelegate>
@property(nonatomic,strong)UIScrollView* mainScrollView;
@property(nonatomic,strong)GYBuyDetailView* buyView;
@property(nonatomic,strong)GYBuyDetailListView* ListView;
@property(nonatomic,strong)GYhomeBuyDetailViewModel* BuyDetailViewModel;
@property(nonatomic,strong)DWSegmentedControl *test;

@end

@implementation GYHomeBuyDetailVC
#pragma mark - getter and setter
-(GYhomeBuyDetailViewModel *)BuyDetailViewModel
{
    if (!_BuyDetailViewModel) {
        _BuyDetailViewModel=[[GYhomeBuyDetailViewModel alloc] init];
    }
    return _BuyDetailViewModel;
}
-(UIScrollView *)mainScrollView{
    
    if (!_mainScrollView) {
        _mainScrollView=[[UIScrollView alloc] init];
        _mainScrollView.frame=CGRectMake(0, 0, self.view.width, self.view.height-0-64);
        _mainScrollView.backgroundColor=[UIColor whiteColor];
        _mainScrollView.contentSize=CGSizeMake(self.view.width, _mainScrollView.height);
        _mainScrollView.pagingEnabled=YES;
        _mainScrollView.delegate=self;
        _mainScrollView.showsVerticalScrollIndicator=NO;
        _mainScrollView.showsHorizontalScrollIndicator=NO;
    }
    return _mainScrollView;
}
-(GYBuyDetailView *)buyView
{
    if (!_buyView) {
        _buyView=[[GYBuyDetailView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, _mainScrollView.height)];
        [_buyView refreshTable:self.passModel];
        
    }
    return _buyView;
}
-(GYBuyDetailListView *)ListView
{
    if (!_ListView) {
        _ListView=[[GYBuyDetailListView alloc] initWithFrame:CGRectMake(self.view.width, 0, self.view.width, _mainScrollView.height)];
    }
    return _ListView;
}
-(DWSegmentedControl *)test
{
    if (!_test)
    {
        _test = [[DWSegmentedControl alloc] initWithFrame:CGRectMake(58, 7.5, self.view.frame.size.width-58*2, 28)];
        _test.backgroundColor = RGBACOLOR(251, 251, 251, 1);
        _test.selectedViewColor = Main_Color_Red;
        _test.normalLabelColor = Main_Color_Red;
        _test.delegate = self;
        _test.titles = @[@"求购详情",@"选择产品供货"];
    }
    return _test;
}
#pragma mark - event
#pragma mark - method

-(void)viewModelBand
{
    @WeakSelf;
    
    GCBaseObservationModel *loginOb = [GCBaseObservationModel new];
    loginOb.observation = [GYLoginViewModel shareInterface];
    loginOb.keyPath = @"loginstatus";
    loginOb.handler = ^(NSString *keyPath) {
        [selfp.buyView refreshTable:self.passModel];
        [selfp.ListView setOriginData:[NSString stringWithFormat:@"%ld",self.BuyDetailViewModel.buyDetailInfoModel.protypeid]];
    };
    [self registObservation:loginOb];
    
    GCBaseObservationModel* saleOber=[GCBaseObservationModel new];
    saleOber.keyPath=@"buyDetailInfoModel";
    saleOber.observation=self.BuyDetailViewModel;
    saleOber.handler = ^(NSString *keyPath) {
        [selfp.buyView refreshTable:self.BuyDetailViewModel.buyDetailInfoModel];
        [selfp.ListView setOriginData:[NSString stringWithFormat:@"%ld",self.BuyDetailViewModel.buyDetailInfoModel.protypeid]];
    };
    [self registObservation:saleOber];
    
    GCBaseObservationModel* buyOber=[GCBaseObservationModel new];
    buyOber.keyPath=@"productData";
    buyOber.observation=self.BuyDetailViewModel;
    buyOber.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadData];
    };
    [self registObservation:buyOber];
    
    GCBaseObservationModel* marketOber=[GCBaseObservationModel new];
    marketOber.keyPath=@"isCollect";
    marketOber.observation=self.BuyDetailViewModel;
    marketOber.handler = ^(NSString *keyPath) {
        [selfp.buyView refreshCollect:selfp.BuyDetailViewModel.isCollect];
    };
    [self registObservation:marketOber];

}
-(void)loadData
{
    NSMutableDictionary* para=[[NSMutableDictionary alloc] init];
    para[@"id"]=[NSString stringWithFormat:@"%@",_passID];
    [self.BuyDetailViewModel requestDetai:para];
    [self.BuyDetailViewModel requestProductList:nil];
    
    NSMutableDictionary* Colpara=[[NSMutableDictionary alloc] init];
    Colpara[@"type"]=@"2";
    Colpara[@"productid"]=[NSString stringWithFormat:@"%@",_passID];
    [self.BuyDetailViewModel requestCheckCollect:Colpara];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title=@"求购信息";
    [self viewModelBand];
    [self loadData];
   // [self.view addSubview:self.test];
    [self.mainScrollView addSubview:self.buyView];
//    [self.mainScrollView addSubview:self.ListView];
    _ListView.PassID=_passID;
    [self.view addSubview:self.mainScrollView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
-(void)dw_segmentedControl:(DWSegmentedControl *)control didSeletRow:(NSInteger)row
{
    if (row==1)
    {
        if ([[GYLoginViewModel shareInterface] roleType]==1)
        {
            if ([[GYLoginViewModel shareInterface] loginstatus])
            {
                [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*row, 0, self.view.width, _mainScrollView.height) animated:NO];
            }else
            {
                [[GCPresenter shareInterface] loadLogin];
                [_test setSelectItem:0];
            }
        }else
        {
            [FTIndicator showErrorWithMessage:@"您还不是革优认证会员,请注册并认证~"];
        }
    }else
    {
        [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*row, 0, self.view.width, _mainScrollView.height) animated:NO];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==_mainScrollView)
    {
        [_test setScrollViewOffset:scrollView.contentOffset.x/self.view.width];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_mainScrollView)
    {
        if (scrollView.contentOffset.x/self.view.width==1)
        {
            
            if ([[GYLoginViewModel shareInterface] roleType]==1)
            {
                if (![[GYLoginViewModel shareInterface] loginstatus])
                {
                    [[GCPresenter shareInterface] loadLogin];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [_mainScrollView scrollRectToVisible:CGRectMake(0, 0, self.view.width, _mainScrollView.height) animated:NO];
                    });
                }
            }else
            {
                [FTIndicator showErrorWithMessage:@"您还不是革优认证会员,请注册别并认证~"];
            }
        }
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
